const Status = Object.freeze({
  SUCCESS: 200,
  PARAMETER_ERROR: 400,
  UNKNOWN_ABNORMAL: 500,
});

const Message = Object.freeze({
  SUCCESS: '成功',
  PARAMETER_ERROR: '参数错误',
  UNKNOWN_ABNORMAL: '未知错误',
});

class Result {
   data = {
     list: [],
     total: 0,
   }
   constructor({ code, msg, data } = {}) {
     this.code = code;
     this.msg = msg;
     this.data.list = data;
   }

   static success(data) {
     const r = new Result();
     r.code = Status.SUCCESS;
     r.msg = Message.SUCCESS;
     r.data.list = data;
     return r;
   }

   static error() {
     const r = new Result();
     r.code = Status.UNKNOWN_ABNORMAL;
     r.msg = Message.PARAMETER_ERROR;
     return r;
   }

   setCode(code) {
     this.code = code;
     return this;
   }

   setMsg(msg) {
     this.msg = msg;
     return this;
   }

   setData(data) {
     this.data.list = data;
     return this;
   }

   setTotal(total) {
     this.data.total = total;
     return this;
   }
}

module.exports = { Result, Message, Status };
